package cn.ffcs.businessrequest.dao;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.businessrequest.model.BusinessRequest;

/**
 * 
 * 业务需求交互单DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class BusinessRequestDAO<D extends BusinessRequest> extends BaseDAO<D> implements IBusinessRequestDAO<D>
{

    /**
     * 获取共享资料实体类
     * @param b
     * @return
     */
    public String getEntityName()
    {
        return new BusinessRequest().getClass().getName();
    }
    
    public List findOrganized(HashMap<String, Object> conds)
    {
        String hql = "select distinct businessRequest from BusinessRequest "
            +"businessRequest,ProcTranRecord procTranRecord where "
            +"procTranRecord.refEntityId=businessRequest.id and businessRequest.end>sysdate ";
        
        hql = "select distinct businessRequest from BusinessRequest "
            +"businessRequest,ProcTranRecord procTranRecord where "
            +"procTranRecord.procinstId=businessRequest.procinstId and businessRequest.end>sysdate ";
        if (conds != null)
        {
            //遍历参数表
            Iterator i = conds.entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (conds.get(key) != null
                        && !StringUtils.isEmpty(conds.get(key).toString()))
                {
                    if (key.equalsIgnoreCase("businessRequest.name")){
                        hql+=" and "+key+" like '%"+ conds.get(key)+"%'";
                    }else{
                        hql+=" and "+key+"='"+ conds.get(key)+"'";
                    }
                }
            }
        }
        hql+=" order by businessRequest.id desc";
        return this.getSession().createQuery(hql).list();
    }
    /**
     * 查找所有需求单
     * @param conds
     * @return
     */
    public List findAllBusiness(HashMap<String, Object> conds)
    {
        String hql = "select distinct businessRequest from BusinessRequest "
            +"businessRequest where "
            +" businessRequest.end>sysdate and businessRequest.procinstType='7'" +
            " and businessRequest.auditResult='1' and businessRequest.isNew!='1' ";
        if (conds != null)
        {
            //遍历参数表
            Iterator i = conds.entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (conds.get(key) != null
                        && !StringUtils.isEmpty(conds.get(key).toString()))
                {
                    if (key.equalsIgnoreCase("businessRequest.name")){
                        hql+=" and "+key+" like '%"+ conds.get(key)+"%'";
                    }else{
                        hql+=" and "+key+"='"+ conds.get(key)+"'";
                    }
                }
            }
        }
        hql+=" order by businessRequest.id desc";
        return this.getSession().createQuery(hql).list();
    }
    public List findBusinessRequestByConds(HashMap<String, Object> conds){
        
        String hql="";
        return this.getSession().createQuery(hql).list();
    }
}
